package com.example.sprintboot.controller;

import java.util.List;

import com.example.sprintboot.common.Result;
import com.example.sprintboot.dto.CategoryQueryDto;
import com.example.sprintboot.dto.PageDto;
import com.example.sprintboot.entity.Category;
import com.example.sprintboot.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@RestController
@RequestMapping("/category")
@Tag(name = "分类管理", description = "分类相关的API接口")
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Autowired
    private InternalResourceViewResolver defaultViewResolver;

    @Operation(summary = "查询所有分类", description = "获取所有分类数据")
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Category> list = categoryService.selectAll();
        return Result.success(list);
    }

    @Operation(summary = "根据ID查询分类", description = "根据分类ID查询分类详情")
    @PostMapping("/selectById")
    public Result selectById(@RequestBody CategoryQueryDto queryDto){
        Category category = categoryService.selectById(queryDto.getId());
        return Result.success(category);
    }

    @Operation(summary = "添加分类", description = "新增一个分类")
    @PostMapping("/add")
    public Result add(@RequestBody Category category){
        categoryService.add(category);
        return Result.success();
    }

    @Operation(summary = "更新分类", description = "更新分类信息")
    @PutMapping("/update")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return Result.success();
    }
    @Operation(summary = "删除分类", description = "根据ID删除分类")
    @DeleteMapping("/delete/{id}")
    public Result delete(@Parameter(description = "分类ID", required = true) @PathVariable Integer id){
        categoryService.delete(id);
        return  Result.success();
    }

    @Operation(summary = "分页查询分类", description = "分页获取分类列表")
    @PostMapping("/page")
    public Result page(@RequestBody PageDto pageDto){
        PageInfo<Category> pageInfo = categoryService.page(pageDto.getPageNum(), pageDto.getPageSize());
        return Result.success(pageInfo);
    }

    @Operation(summary = "条件查询分类", description = "根据名称等条件分页查询分类")
    @PostMapping("/query")
    public Result query(@RequestBody CategoryQueryDto queryDto) {
        PageInfo<Category> pageInfo = categoryService.query(queryDto.getName(), queryDto.getPageNum(), queryDto.getPageSize());
        return Result.success(pageInfo);
    }
}
